package com.yiwenup.leetcode.hot;

/**
 * https://leetcode-cn.com/problems/rotate-image/
 * <pre>
 *     输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]
 *     输出：[[7,4,1],[8,5,2],[9,6,3]]
 * </pre>
 **/
public class No0048 {

    /**
     * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
     * 内存消耗：40.1 MB, 在所有 Java 提交中击败了21.09%的用户
     */
    public void rotate1(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < (n >> 1); i++) {
            for (int j = 0; j < (n + 1) >> 1; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[n - j - 1][i];
                matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
                matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
                matrix[j][n - i - 1] = tmp;
            }
        }
    }

    /**
     * 执行用时：0 ms, 在所有 Java 提交中击败了100.00%的用户
     * 内存消耗：40.8 MB, 在所有 Java 提交中击败了5.04%的用户
     */
    public void rotate2(int[][] matrix) {
        int n = matrix.length;
        int[][] tmp = new int[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp[j][n - i - 1] = matrix[i][j];
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = tmp[i][j];
            }
        }
    }
}